import 'dart:convert';

import 'package:baselib/utils/sp_util.dart';
import 'package:framelib/utils/constant_util.dart';

class AppConfig{
  static final AppConfig _singleton = AppConfig._internal();

  static AppConfig get instance => AppConfig();

  factory AppConfig(){
    return _singleton;
  }
  AppConfig._internal() {}

  int SUCCESS_CODE = 200;
  int UNAUTHORIZED = 401;

  String baseUrl = 'http://192.168.0.103:38010';
  String privacyBaseUrl = 'http://192.168.0.103:38010';


  clearSp()async{
    await setToken(token: '');
    await setUserId(userId: (-1));
    await setUserName(userName: '');
    await setPhone(phone: '');
    await setEmail(email: '');
    await setPassword(password: '');
    await setAvatar(avatar: '');
    await setCurBookId(curBookId: '');
    await setPermissions(value: []);
    await setRoles(value: []);
    await setUserEntity(value: null);
    await SpUtil.clear();
  }


  String _token = '';
  String get token => ((_token??'').isNotEmpty?_token:(SpUtil.getString(ConstantUtil.token)??''));
  Future<AppConfig> setToken({String? token})async{
    this._token = token??'';
    await SpUtil.putString(ConstantUtil.token, this._token);
    return this;
  }

  int _userId = -1;
  int get userId => ((_userId??(-1))!=(-1)?_userId:(SpUtil.getInt(ConstantUtil.userId)??(-1)));
  Future<AppConfig> setUserId({int? userId})async{
    this._userId = userId??(-1);
    await SpUtil.putInt(ConstantUtil.userId, this._userId);
    return this;
  }
  String _userName = '';
  String get userName => ((_userName??'').isNotEmpty?_userName:(SpUtil.getString(ConstantUtil.userName)??''));
  Future<AppConfig> setUserName({String? userName})async{
    this._userName = userName??'';
    await SpUtil.putString(ConstantUtil.userName, this._userName);
    return this;
  }
  String _phone = '';
  String get phone => ((_phone??'').isNotEmpty?_phone:(SpUtil.getString(ConstantUtil.phone)??''));
  Future<AppConfig> setPhone({String? phone})async{
    this._phone = phone??'';
    await SpUtil.putString(ConstantUtil.phone, this._phone);
    return this;
  }
  String _email = '';
  String get email => ((_email??'').isNotEmpty?_email:(SpUtil.getString(ConstantUtil.email)??''));
  Future<AppConfig> setEmail({String? email})async{
    this._email = email??'';
    await SpUtil.putString(ConstantUtil.email, this._email);
    return this;
  }
  String _password = '';
  String get password => ((_password??'').isNotEmpty?_password:(SpUtil.getString(ConstantUtil.password)??''));
  Future<AppConfig> setPassword({String? password})async{
    this._password = password??'';
    await SpUtil.putString(ConstantUtil.password, this._password);
    return this;
  }
  String _avatar = '';
    String get avatar => ((_avatar??'').isNotEmpty?_avatar:(SpUtil.getString(ConstantUtil.avatar)??''));
    Future<AppConfig> setAvatar({String? avatar})async{
      this._avatar = avatar??'';
      await SpUtil.putString(ConstantUtil.avatar, this._avatar);
      return this;
    }

  String _curBookId = '';
  String get curBookId => ((_curBookId??'').isNotEmpty?_curBookId:(SpUtil.getString(ConstantUtil.curBookId)??''));
  Future<AppConfig> setCurBookId({String? curBookId})async{
    this._curBookId = curBookId??'';
    await SpUtil.putString(ConstantUtil.curBookId, this._curBookId);
    return this;
  }

  List<String> _permissions = [];
  List<String> get permissions => (_permissions??[]).isNotEmpty?_permissions:(json.decode(SpUtil.getString(ConstantUtil.permissions)??''));
  Future<AppConfig> setPermissions({List<String>? value}) async{
    this._permissions = value??[];
    await SpUtil.putString(ConstantUtil.permissions, json.encode(this._permissions));
    return this;
  }

  List<String> _roles = [];
  List<String> get roles => (_roles??[]).isNotEmpty?_roles:(json.decode(SpUtil.getString(ConstantUtil.roles)??''));
  Future<AppConfig> setRoles({List<String>? value}) async{
    this._roles = value??[];
    await SpUtil.putString(ConstantUtil.roles, json.encode(this._roles));
    return this;
  }

  var _userEntity;

  get userEntity => _userEntity;

  Future<AppConfig> setUserEntity({var value}) async{
    _userEntity = value;
    return this;
  }

  bool isLogin(){
    return token.isNotEmpty;
  }
}